package week4.day22;

import java.io.*;
import java.util.Scanner;

/**
 * @Author fengzihao
 * @Date 2024-05-06 19:12
 */
public class IOPractice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件地址：");
        String name = scanner.nextLine();
        System.out.println("请输入要存储的字符串：");
        String str = scanner.nextLine();
        File file = new File(name);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(str.getBytes("UTF-8"));
        } catch (FileNotFoundException e) {
            try {
                file.createNewFile();
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(str.getBytes("UTF-8"));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
